<?php

namespace App\Model\Examples;

use PhalApi\Model\NotORMModel as NotORM;

/**
 *
 * 一般订票管理
 */
class UserOrder extends NotORM
{

    protected function getTableName($id)
    {
        return 'user_order';
    }


    public function addUserOrder($username, $tel, $person_num, $remark, $spot_type_id)
    {
        $total = $this->getORM()
            ->where('is_hot', '1');

        return $total;
    }

    public function getAllCitysByWord($word)
    {
        $total = $this->getORM();
        if (!empty($word)) {
            $total = $total->where('cityname LIKE ?', '%' . $word . '%');
        }
        return $total;
    }

    //获得当前拼音下的城市列表
    public function getCitysByPingName($word)
    {
        $total = $this->getORM()
            ->select('id,cityid,cityname')
            ->where('pingname', $word);
        return $total;
    }

    public function getOrderDetails($id)
    {
        $sql1 = 'SELECT * FROM spot_view,user_order WHERE spot_view.id = user_order.spot_id  and user_order.id ='.$id;
        $rs = $this->getORM()->queryAll($sql1);
        return $rs;
    }

    public function getListItems($page, $perpage, $status_id, $openid,$word)
    {
        if ($openid) {
            $sql1 = 'SELECT spot_view.*,user_order.*,spot_view.id as spot_view_id
                  FROM spot_view,user_order WHERE spot_view.id = user_order.spot_id
                  and user_order.tel like :tel
                  and openid=:openid and user_order.status_id !=:status_id order by create_time desc limit :start,:size ';
            $params = array(':tel'=>'%'.$word.'%',':openid' => $openid, ':status_id' => $status_id
            , ':start' => ($page - 1) * $perpage, ':size' => $perpage);
        } else {
            $sql1 = 'SELECT spot_view.*,user_order.*,spot_view.id as spot_view_id
                  FROM spot_view,user_order WHERE spot_view.id = user_order.spot_id
                  and user_order.tel like :tel
                  and user_order.status_id !=:status_id order by create_time desc limit :start,:size';
            $params = array(':tel'=>'%'.$word.'%',':status_id' => $status_id
            , ':start' => ($page - 1) * $perpage, ':size' => $perpage);
        }
        $rs = $this->getORM()->queryAll($sql1, $params);
        return $rs;
    }

    public function getListTotal($status_id, $openid,$word)
    {
        $total = $this->getORM();
        if (!empty($openid)) {
            $total = $total->where('openid', $openid);
        }
        if (!empty($word)) {
            $total = $total->where('tel like ?', '%'.$word.'%');
        }
        $total = $total->where('status_id !=?', $status_id)
            ->count('id');

        return intval($total);
    }
}
